package com.hue.concurrent.day02;

/*
    synchronized使用
 */
public class Demo02 {
    public static int a = 0;

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 3000; i++) {
                increment();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 3000; i++) {
                decrement();
            }
        });

        t1.start();
        t2.start();

        Thread.sleep(1000);
        System.out.println(a);
    }

    public synchronized static void increment() {
        a++;
    }

    public synchronized static void decrement() {
        a--;
    }
}
